﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.Users;
using Zt.QmtqDemo.ConfmPlan;

namespace Zt.QmtqDemo.EntityFrameworkCore
{
    public static class QmtqDemoDbContextModelCreatingExtensions
    {
        public static void ConfigureQmtqDemo(this ModelBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));

            builder.Entity<ConformPlan>(b =>
            {
                b.ToTable("TPOPL001");

                b.ConfigureByConvention();

                b.Property(x => x.ConfmPlanNo).IsRequired().HasMaxLength(ConfmPlanConsts.MaxNameLength);
                b.Property(x => x.ConfmStatus).IsRequired().HasMaxLength(ConfmPlanConsts.MaxConfmStatusLength);
                b.Property(x => x.TotalMatNum).IsRequired();
                b.Property(x => x.TotalMatWt).IsRequired();
                b.Property(x => x.OrderNO).IsRequired().HasMaxLength(ConfmPlanConsts.MaxOrderNoLength);  
                b.Property(x => x.MoveInStockNO).HasMaxLength(ConfmPlanConsts.MaxMoveInStockNOLength);

                b.HasMany(x => x.ConfmPlanItems).WithOne()
                    .HasForeignKey(x => new { x.ConfmPlanId })
                    .IsRequired();
            });

            builder.Entity<ConformPlanItem>(b =>
            {
                b.ToTable("TPOPL002");

                b.HasKey(x => new { x.ConfmPlanId, x.MatNo });
            });
        }
    }
}